package cn.zch.util;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StringUtil {
    public static String T = "	";
    public static String S = " ";
    public static String windows = "windows";
    public static String linux = "linux";
    private static final Pattern PATTERN = Pattern.compile("[0-9]*");
    /**
     * 获取请求基路径
     */
    public static String requestBase(HttpServletRequest request) {
        String url = request.getRequestURI();
        url = url.replace("/WEB-INF/page", "");
        int last = url.lastIndexOf("/");
        url = url.substring(0, last);
        return url;
    }
    /**
     * 获取请求路径
     */
    public static String requestPath(HttpServletRequest request) {
        String url = request.getRequestURI();
        url = url.replace("/WEB-INF/page", "");
        int last = url.lastIndexOf(".");
        url = url.substring(0, last);
        return url;
    }
    /**
     * 获取转发路径
     */
    public static String requestPath(HttpServletRequest request, String path) {
        String url = request.getRequestURI();
        url = url.replace("/WEB-INF/page", "");
        int last = url.lastIndexOf("/");
        url = url.substring(0, last + 1) + path;
        return url;
    }
    /**
     * byte转int
     */
    public static Integer byteToInt(byte b) {
        return b & 0xFF;
    }
    /**
     * byte转16进制字符
     */
    private static String toHexString(byte b) {
        String hex = Integer.toHexString(b & 0xFF);
        if (hex.length() == 1) {
            hex = "0" + hex;
        }
        return hex;
    }
    /**
     * 返回数字左补零字符串，如1返回"01"
     */
    public static String leftPad(Integer str, Integer num) {
        String temp = Integer.toString(str);
        num = num - temp.length();
        for (int i = 0; i < num; i++) {
            temp = "0" + temp;
        }
        return temp;
    }
    /**
     * 转换成16进制字符串再左补零
     */
    public static String hexLeftPad(BigDecimal str, Integer num) {
        String temp = Integer.toHexString(str.intValue()).replace("ffffff", "");
        num = num - temp.length();
        for (int i = 0; i < num; i++) {
            temp = "0" + temp;
        }
        return temp;
    }
    /**
     * 转换成16进制字符串再左补零
     */
    public static String hexLeftPad(int str, Integer num) {
        String temp = Integer.toHexString(str).replace("ffffff", "");
        num = num - temp.length();
        for (int i = 0; i < num; i++) {
            temp = "0" + temp;
        }
        return temp;
    }
    /**
     * long转换成16进制字符串再左补零
     */
    public static String hexLeftPad(long str, Integer num) {
        String temp = Long.toHexString(str).replace("ffffff", "");
        num = num - temp.length();
        for (int i = 0; i < num; i++) {
            temp = "0" + temp;
        }
        return temp;
    }
    /**
     * 16进制数字右补零
     */
    public static String hexRightPad(Integer str, Integer num) {
        String temp = Integer.toHexString(str);
        num = num - temp.length();
        for (int i = 0; i < num; i++) {
            temp += "0";
        }
        return temp;
    }
    /**
     * 字符串左补零
     */
    public static String strLeftPad(String str, Integer num) {
        num = num - str.length();
        for (int i = 0; i < num; i++) {
            str = "0" + str;
        }
        return str;
    }
    /**
     * 字符串左补字符
     */
    public static String strLeftPadWithChar(String str, Integer num, String c) {
        num = num - str.length();
        for (int i = 0; i < num; i++) {
            str = c + str;
        }
        return str;
    }
    /**
     * 字符串右补零
     */
    public static String strRightPad(String str, Integer num) {
        num = num - str.length();
        for (int i = 0; i < num; i++) {
            str += "0";
        }
        return str;
    }
    /**
     * 转换日期格式为16进制字符串 例：“2009-12-04” -> “9C04”
     * 修改2009-07-06：二进制前7位表示年，下4位月，最后5位表示日如"2009-07-06" -> "12E6"
     */
    public static String dateToHexStr(Calendar date) {
        String year = String.valueOf(date.get(Calendar.YEAR));
        byte tmYear = (byte) ((int) Integer.valueOf(year.substring(2)));
        byte tmMonth = (byte) (date.get(Calendar.MONTH) + 1);
        byte tmDay = (byte) date.get(Calendar.DAY_OF_MONTH);
        String tmpStr = Integer.toHexString((tmYear << 1) | (tmMonth >> 3));
        if (tmpStr.length() == 1) {
            tmpStr = "0" + tmpStr;
        }
        String tmpStr2 = Integer.toHexString((byte) (tmMonth << 5 | tmDay));
        if (tmpStr2.length() >= 2) {
            tmpStr2 = tmpStr2.substring(tmpStr2.length() - 2);
        }
        if (tmpStr2.length() == 1) {
            tmpStr2 = "0" + tmpStr2;
        }
        tmpStr += tmpStr2;
        if (tmpStr.length() == 4) {
            return tmpStr;
        }
        return "0000";
    }
    /**
     * 转换日期格式为16进制字符串 例：“2009-12-04” -> “9C04”
     * 修改2009-07-06：二进制前7位表示年，下4位月，最后5位表示日如"2009-07-06" -> "12E6"
     */
    public static String dateStrToHexStr(String dateStr) {
        Calendar date = Calendar.getInstance();
        String[] dateStrs = dateStr.split("-");
        date.set(StringUtil.objToInt(dateStrs[0]), StringUtil.objToInt(dateStrs[1]), StringUtil.objToInt(dateStrs[2]));
        date.add(Calendar.MONTH, -1);
        String year = String.valueOf(date.get(Calendar.YEAR));
        byte tmYear = (byte) ((int) Integer.valueOf(year.substring(2)));
        byte tmMonth = (byte) (date.get(Calendar.MONTH) + 1);
        byte tmDay = (byte) date.get(Calendar.DAY_OF_MONTH);
        String tmpStr = Integer.toHexString((tmYear << 1) | (tmMonth >> 3));
        if (tmpStr.length() == 1) {
            tmpStr = "0" + tmpStr;
        }
        String tmpStr2 = Integer.toHexString(tmMonth << 5 | tmDay);
        tmpStr2 = tmpStr2.substring(tmpStr2.length() - 2);
        if (tmpStr2.length() == 1) {
            tmpStr2 = "0" + tmpStr2;
        }
        tmpStr += tmpStr2;
        if (tmpStr.length() == 4) {
            return tmpStr;
        }
        return "0000";
    }
    /**
     * 转换16进制字符串为日期格式 例：“9C04” -> “2009-12-04”
     * 修改2009-07-06：二进制前7位表示年，下4位月，最后5位表示日如"12E6" -> "2009-07-06"
     */
    public static String dateFromHexStr(String hexStr) {
        int tm1 = Integer.parseInt(hexStr.substring(0, 2), 16);
        int tm2 = Integer.parseInt(hexStr.substring(2), 16);
        int tmYear = (tm1 & 0XFE) >> 1;
        int tmMonth = (tm1 & 0X01) << 3 | (tm2 >> 5);
        int tmDay = tm2 & 0X1F;
        String year = "20" + leftPad(tmYear, 2);
        String month = leftPad(tmMonth, 2);
        String day = leftPad(tmDay, 2);
        String tmpDate = year + "-" + month + "-" + day;
        if (tmpDate.length() == 10) {
            return tmpDate;
        }
        return "0000-00-00";
    }
    /**
     * 2进制字符串转16进制字符串
     */
    public static String binaryHexStr(String binary) {
        //		String[] strs = binary.split(" ");
        //		String result = "";
        //		for(String s : strs){
        //			String hex = Integer.toString(Integer.parseInt(s,2),16);
        //			result += hex;
        //		}
        //		if(result.length() == 1){
        //			result = "0" + result;
        //		}
        String[] strs = binary.split(" ");
        StringBuilder result = new StringBuilder();
        for (String s : strs) {
            String hex = Integer.toString(Integer.parseInt(s, 2), 16);
            result.append(hex);
        }
        if (result.length() == 1) {
            result.insert(0, "0");
        }
        return result.toString();
    }
    /**
     * 四字节时间戳
     */
    public static String timeToHexStr() {
        //		String tempStr = "";
        //		Date date = new Date("2000/01/01");
        //		long time = (System.currentTimeMillis() - date.getTime()) / 1000;
        //		for(int i = 24;i >= 0;i -= 8){
        //			String tmpStr1 = Integer.toHexString((byte)((time >> i) & 0xff)).replace("ffffff","");
        //			if(tmpStr1.length() == 1){
        //				tempStr = tempStr + "0" + tmpStr1;
        //			} else{
        //				tempStr += tmpStr1;
        //			}
        //		}
        StringBuilder tempStr = new StringBuilder();
        Date date = new Date("2000/01/01");
        long time = (System.currentTimeMillis() - date.getTime()) / 1000;
        for (int i = 24; i >= 0; i -= 8) {
            String tmpStr1 = Integer.toHexString((byte) ((time >> i) & 0xff)).replace("ffffff", "");
            if (tmpStr1.length() == 1) {
                tempStr.append("0").append(tmpStr1);
            }
            else {
                tempStr.append(tmpStr1);
            }
        }
        return tempStr.toString();
    }
    /**
     * 得到字节数组
     */
    public static byte[] strTobytes(String str) {
        byte[] b = new byte[str.length() / 2];
        for (int i = 0; i < str.length() / 2 - 2; i++) {
            String s = str.substring(i * 2, i * 2 + 2);
            b[i] = (byte) Integer.parseInt(s, 16);
        }
        return b;
    }
    public static byte[] yzMjStrToBytes(String str) {
        str = "7E" + str + "7E";
        byte[] b = new byte[str.length() / 2];
        for (int i = 0; i < str.length() / 2 - 2; i++) {
            String s = str.substring(i * 2, i * 2 + 2);
            b[i] = (byte) Integer.parseInt(s, 16);
        }
        return b;
    }
    /**
     * 获取国标码
     */
    public static String strToGB2312(String str) {
        //		String result = "";
        //		byte[] strByte;
        //		try{
        //			strByte = str.getBytes("GB2312");
        //			for(byte b : strByte){
        //				result += StringUtil.hexLeftPad(b,2);
        //			}
        //		} catch(UnsupportedEncodingException e){
        //			e.printStackTrace();
        //		}
        StringBuilder result = new StringBuilder();
        byte[] strByte;
        try {
            strByte = str.getBytes("GB2312");
            for (byte b : strByte) {
                result.append(StringUtil.hexLeftPad(b, 2));
            }
        }
        catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return StringUtil.strRightPad(result.toString(), 32);
    }
    /**
     * 获取BCD码int
     */
    public static Integer byteToBCDInt(byte b) {
        return Integer.parseInt(toHexString(b));
    }
    /**
     * 日期格式化
     */
    public static String dateFormat(Date date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }
    /**
     * 从字节数组中获取指定位置的hex字符形式数据 byte[]b= {00, 11, 22, 33, 44}; byteToHexStr(1, 3, byte[] b) 结果11 22 33
     */
    public static String getHexStrFromBytes(Integer begin, Integer end, byte[] b) {
        StringBuilder baseInfoStr = new StringBuilder();
        for (int i = begin; i <= end; i++) {
            String hex = Integer.toHexString(b[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            baseInfoStr.append(hex);
        }
        return baseInfoStr.toString();
    }
    /**
     * 转换卡hex字节数据到int
     */
    public static Integer hexToInt(Integer begin, Integer end, byte[] b) {
        return Integer.parseInt(getHexStrFromBytes(begin, end, b), 16);
    }
    /**
     * 转换卡hex字节数据到long
     */
    public static Long hexToLong(Integer begin, Integer end, byte[] b) {
        return Long.parseLong(getHexStrFromBytes(begin, end, b), 16);
    }

    /**
     * object类型int字符串转Integer
     */
    public static Integer objToInt(Object obj) {
        if (obj == null) {
            return 0;
        }
        return Integer.valueOf(obj.toString());
    }
    /**
     * object类型long字符串转Long
     */
    public static Long objToLong(Object obj) {
        if (obj == null) {
            return (long) 0;
        }
        return Long.valueOf(obj.toString());
    }
    /**
     * object类型float字符串转Float
     */
    public static Float objToFloat(Object obj) {
        if (obj == null) {
            return (float) 0;
        }
        return Float.valueOf(obj.toString());
    }
    /**
     * Object转String
     */
    public static String objToString(Object obj) {
        if (obj != null) {
            return obj.toString();
        }
        else {
            return "";
        }
    }
    /**
     * 姓名简拼
     */
    public static String toJP(String c) throws UnsupportedEncodingException {
        char[] chars = c.toCharArray();
        StringBuilder sb = new StringBuilder("");
        for (char aChar : chars) {
            sb.append(getJP(aChar));
        }
        return sb.toString().toUpperCase();
    }
    /**
     * 获取简拼
     */
    private static String getJP(char c) throws UnsupportedEncodingException {
        byte[] array = String.valueOf(c).getBytes("gbk");
        if (array.length < 2) {
            return String.valueOf(c);
        }
        int i = (short) (array[0] - '\0' + 256) * 256 + ((short) (array[1] - '\0' + 256));
        if (i < 0xB0A1) {
            return String.valueOf(c);
        }
        if (i < 0xB0C5) {
            return "a";
        }
        if (i < 0xB2C1) {
            return "b";
        }
        if (i < 0xB4EE) {
            return "c";
        }
        if (i < 0xB6EA) {
            return "d";
        }
        if (i < 0xB7A2) {
            return "e";
        }
        if (i < 0xB8C1) {
            return "f";
        }
        if (i < 0xB9FE) {
            return "g";
        }
        if (i < 0xBBF7) {
            return "h";
        }
        if (i < 0xBFA6) {
            return "j";
        }
        if (i < 0xC0AC) {
            return "k";
        }
        if (i < 0xC2E8) {
            return "l";
        }
        if (i < 0xC4C3) {
            return "m";
        }
        if (i < 0xC5B6) {
            return "n";
        }
        if (i < 0xC5BE) {
            return "o";
        }
        if (i < 0xC6DA) {
            return "p";
        }
        if (i < 0xC8BB) {
            return "q";
        }
        if (i < 0xC8F6) {
            return "r";
        }
        if (i < 0xCBFA) {
            return "s";
        }
        if (i < 0xCDDA) {
            return "t";
        }
        if (i < 0xCEF4) {
            return "w";
        }
        if (i < 0xD1B9) {
            return "x";
        }
        if (i < 0xD4D1) {
            return "y";
        }
        if (i < 0xD7FA) {
            return "z";
        }
        return String.valueOf(c);
    }
    /**
     * 获取水控时间段hex
     */
    public static String getWaterRateTime(String beginTime, String endTime) {
        String hex = "";
        String[] time = beginTime.split(":");
        hex += StringUtil.hexLeftPad(Integer.valueOf(time[0]), 2);
        hex += StringUtil.hexLeftPad(Integer.valueOf(time[1]), 2);
        time = endTime.split(":");
        hex += StringUtil.hexLeftPad(Integer.valueOf(time[0]), 2);
        hex += StringUtil.hexLeftPad(Integer.valueOf(time[1]), 2);
        return hex;
    }

    /**
     * 水控授权16个卡类型转hex字符串2字节
     */
    public static String getWaterCardTypesHexStr(String str) {
        String result = "";
        for (int i = 7; i >= 0; i--) {
            if (str.indexOf("," + i + ",") == -1) {
                result += "0";
            }
            else {
                result += "1";
            }
        }
        for (int i = 15; i >= 8; i--) {
            if (str.indexOf("," + i + ",") == -1) {
                result += "0";
            }
            else {
                result += "1";
            }
        }
        return StringUtil.strLeftPadWithChar(StringUtil.binaryHexStr(result), 4, "0");
    }
    /**
     * 消费机授权16个卡类型转hex字符串2字节
     */
    public static String getPosCardTypesHexStr(String str) {
        String result = "";
        for (int i = 15; i >= 8; i--) {
            if (str.indexOf("," + i + ",") == -1) {
                result += "0";
            }
            else {
                result += "1";
            }
        }
        for (int i = 7; i >= 0; i--) {
            if (str.indexOf("," + i + ",") == -1) {
                result += "0";
            }
            else {
                result += "1";
            }
        }
        return StringUtil.strLeftPadWithChar(StringUtil.binaryHexStr(result), 4, "0");
    }

    /**
     * 获取当前系统时间 zhaochunhui
     */
    public static String getNowTime() {
        // 设置日期格式
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        return df.format(new Date()).toString();
    }
    public static String getNowTimeFortest() {
        // 设置日期格式
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        return df.format(new Date()).toString();
    }
    /**
     * 自动注册用户 生成用户名 名字缩写 编号
     */
    public static String getAboutName() {
        // 设置日期格式
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        return df.format(new Date());
    }
    /**
     * 结算中心查询开始时间
     */
    public static String getNowTimeForDataCenterBg() {
        // 设置日期格式
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        return df.format(new Date());
    }
    /**
     * 结算中心查询结束时间
     */
    public static String getNowTimeForDataCenterEd() {
        // 设置日期格式
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
        return df.format(new Date());
    }
    /**
     * 结算用
     */
    public static String getDateStr(String day, int dayAddNum) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date nowDate = null;
        try {
            nowDate = df.parse(day);
        }
        catch (ParseException e) {
            e.printStackTrace();
        }
        if (null != nowDate) {
            Date newDate2 = new Date(nowDate.getTime() + dayAddNum * 1000);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return simpleDateFormat.format(newDate2);
        }
        else {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        }
    }
    /**
     * 比较日期
     */
    public static int daysBetween(String beginDate, String endDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        long time1 = 0;
        long time2 = 0;
        try {
            cal.setTime(sdf.parse(beginDate));
            time1 = cal.getTimeInMillis();
            cal.setTime(sdf.parse(endDate));
            time2 = cal.getTimeInMillis();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        long between_days = (time2 - time1) / (1000 * 3600 * 24);
        return Integer.parseInt(String.valueOf(between_days));
    }
    /**
     * 获取日期年月日
     */
    public static String getNowTimeForSmartSchoolPackage() {
        // 设置日期格式
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        return df.format(new Date());
    }

    /**
     * 金额空值判断
     */
    public static Object judgeNullObj(Object obj) {
        if (obj == null) {
            obj = new BigDecimal(0);
        }
        else {
            obj = new BigDecimal(obj.toString()).divide(new BigDecimal(100), 2, 4);
        }
        return obj;
    }
    /**
     * 结算中心 单月第一天
     */
    public static String getMonthFirstDay() {
        // 获取当前月第一天：
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, 0);
        // 设置为1号,当前日期既为本月第一天
        c.set(Calendar.DAY_OF_MONTH, 1);
        return format.format(c.getTime());
    }
    /**
     * 当月最后一天
     */
    public static String getMonthLastDay() {
        // 获取当前月最后一天
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
        return format.format(ca.getTime());
    }
    /**
     * 获取系统根路径
     */
    public static String getLogRootPath() {
        String osName = getOsNameLowerCase();
        String rootPath = "";
        if (osName.contains(windows)) {
            rootPath = "E:/LogMessage";
        }
        else if (osName.contains(linux)) {
            rootPath = "/usr/singbon/logs";
        }
        return rootPath;
    }
    /**
     * 获取系统名称
     */
    public static String getOsNameLowerCase() {
        return System.getProperties().getProperty("os.name").toLowerCase();
    }
    /**
     * @param getSthCode 0 获取二维码缓存路径
     */
    public static String getSthDependOnOs(int getSthCode) {
        String osName = getOsNameLowerCase();
        String sth = "";
        if (windows.equals(osName)) {
            if (getSthCode == 0) {
                sth = "E:/LogMessage";
            }
        }
        else if (linux.equals(osName)) {
            if (getSthCode == 0) {
                sth = "/usr/singbon/logs";
            }
        }
        return sth;
    }
    /**
     * 利用正则表达式判断字符串是否是数字
     */
    public static boolean isNumeric(String str) {
        Matcher isNum = PATTERN.matcher(str);
        return isNum.matches();
    }
    /**
     * uuid 注册物理卡号
     */
    public static String getAutoGenerateCardSN() {
        return "FF" + UUID.randomUUID().toString().replace("-", "").toUpperCase().substring(0, 8);
    }
    /**
     * 打印
     */
    public static void println(Object obj) {
    }
    /**
     * 字符串转ASCII
     */
    public static String stringToAscii(String value) {
        StringBuilder sbu = new StringBuilder();
        char[] chars = value.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (i != chars.length - 1) {
                sbu.append((int) chars[i]).append(",");
            }
            else {
                sbu.append((int) chars[i]);
            }
        }
        return sbu.toString();
    }
    /**
     * ASCII转字符串
     */
    public static String asciiToString(String value) {
        StringBuilder sbu = new StringBuilder();
        String[] chars = value.split(",");
        StringBuilder hexStr = new StringBuilder();
        for (String aChar : chars) {
            sbu.append((char) Integer.parseInt(aChar));
            hexStr.append(Integer.toHexString(Integer.parseInt(aChar)));
        }
        System.out.println("hexStr:" + hexStr.toString().toUpperCase());
        return sbu.toString();
    }
    /**
     * ASCII码转换为16进制
     */
    public static String ascii2Hex(String str) {
        char[] chars = str.toCharArray();
        StringBuilder hex = new StringBuilder();
        for (char aChar : chars) {
            hex.append(Integer.toHexString(aChar));
        }
        return hex.toString();
    }
    /**
     * 16进制转换为ASCII
     */
    public static String hex2Ascii(String hex) {
        StringBuilder sb = new StringBuilder();
        AtomicReference<StringBuilder> temp = new AtomicReference<>(new StringBuilder());
        //49204c6f7665204a617661 split into two characters 49, 20, 4c...
        for (int i = 0; i < hex.length() - 1; i += 2) {
            //grab the hex in pairs
            String output = hex.substring(i, (i + 2));
            //convert hex to decimal
            int decimal = Integer.parseInt(output, 16);
            //convert the decimal to character
            sb.append((char) decimal);
            temp.get().append(decimal);
        }
        return sb.toString();
    }
    /**
     * int转byte[]
     * 该方法将一个int类型的数据转换为byte[]形式，因为int为32bit，而byte为8bit所以在进行类型转换时，知会获取低8位，
     * 丢弃高24位。通过位移的方式，将32bit的数据转换成4个8bit的数据。注意 &0xff，在这当中，&0xff简单理解为一把剪刀，
     * 将想要获取的8位数据截取出来。
     * @param i 一个int数字
     * @return byte[]
     */
    public static byte[] int2ByteArray(int i) {
        byte[] result = new byte[4];
        result[0] = (byte) ((i >> 24) & 0xFF);
        result[1] = (byte) ((i >> 16) & 0xFF);
        result[2] = (byte) ((i >> 8) & 0xFF);
        result[3] = (byte) (i & 0xFF);
        return result;
    }
    /**
     * byte[]转int
     * 利用int2ByteArray方法，将一个int转为byte[]，但在解析时，需要将数据还原。同样使用移位的方式，将适当的位数进行还原，
     * 0xFF为16进制的数据，所以在其后每加上一位，就相当于二进制加上4位。同时，使用|=号拼接数据，将其还原成最终的int数据
     * @param bytes byte类型数组
     * @return int数字
     */
    public static int bytes2Int(byte[] bytes) {
        int num = bytes[3] & 0xFF;
        num |= ((bytes[2] << 8) & 0xFF00);
        num |= ((bytes[1] << 16) & 0xFF0000);
        num |= ((bytes[0] << 24) & 0xFF0000);
        return num;
    }
    /**
     * 合并byte[]数组 （不改变原数组）
     * @param byte1
     * @param byte2
     * @return 合并后的数组
     */
    public static byte[] byteMerger(byte[] byte1, byte[] byte2) {
        byte[] byte3 = new byte[byte1.length + byte2.length];
        System.arraycopy(byte1, 0, byte3, 0, byte1.length);
        System.arraycopy(byte2, 0, byte3, byte1.length, byte2.length);
        return byte3;
    }
    /**
     * 截取byte数组   不改变原数组
     * @param b 原数组
     * @param off 偏差值（索引）
     * @param length 长度
     * @return 截取后的数组
     */
    public static byte[] subByte(byte[] b, int off, int length) {
        byte[] b1 = new byte[length];
        System.arraycopy(b, off, b1, 0, length);
        return b1;
    }
}
